package JUC;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 豆包LU
 * @date 2023/5/8 16:46
 */
public class SequentialThreadDemo {
    private ReentrantLock lock = new ReentrantLock();
    private Condition conditionA = lock.newCondition();
    private Condition conditionB = lock.newCondition();
    private Condition conditionC = lock.newCondition();
    private int round = 0;

    public void start() throws InterruptedException {
            ExecutorService executor = Executors.newFixedThreadPool(3);
            executor.execute(new ThreadA());
            executor.execute(new ThreadB());
            executor.execute(new ThreadC());
            executor.shutdown();
    }

    public static void main(String[] args) throws InterruptedException {
        new SequentialThreadDemo().start();
    }

    private class ThreadA implements Runnable {
        @Override
        public void run() {
            for (int j = 1; j <= 10; j++) {
                lock.lock();
                try {
                    while (round != 0) {
                        conditionA.await();
                    }
                    for (int i = 1; i <= 5; i++) {
                        System.out.println("A" + i);
                    }
                    round = 1;
                    conditionB.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    private class ThreadB implements Runnable {
        @Override
        public void run() {
            for (int j = 1; j <= 10; j++) {
                lock.lock();
                try {
                    while (round != 1) {
                        conditionB.await();
                    }
                    for (int i = 1; i <= 10; i++) {
                        System.out.println("B" + i);
                    }
                    round = 2;
                    conditionC.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    private class ThreadC implements Runnable {
        @Override
        public void run() {
            for (int j = 1; j <= 10; j++) {
                lock.lock();
                try {
                    while (round != 2) {
                        conditionC.await();
                    }
                    for (int i = 1; i <= 15; i++) {
                        System.out.println("C" + i);
                    }
                    round = 0;
                    conditionA.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }
}
